package mspbots.qbd.data.desktop;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.qbd.data.BaseReceiver;
import mspbots.qbd.data.desktop.profitandloss.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;

/**
 * data.connector.datasync.quickbooks.AccountReceiver
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2020/5/29
 */
@Log4j2
@Component
@RequiredArgsConstructor
public class ProfitAndLossReceiver extends BaseReceiver {

    private final ProfitAndLossStandardRepository standardRepository;
    private final ProfitAndLossDetailRepository detailRepository;
    private final ProfitAndLossPrevYearCompRepository prevYearCompRepository;
    private final ProfitAndLossYTDCompRepository ytdCompRepository;

    @RabbitListener(queues = "quick-books-desktop.ProfitAndLoss-PrevYearComp.sync", concurrency = "6")
    public void processPrevYearComp(ProfitAndLossPrevYearComp resultNode) {
        this.savePrevYearComp(resultNode);
    }

    private void savePrevYearComp(ProfitAndLossPrevYearComp resultNode) {
        resultNode.setSyncTime(LocalDateTime.now());
        ProfitAndLossPrevYearComp oldStandard = this.prevYearCompRepository.findByTenantIdAndRowNumberAndTimePeriod(
                resultNode.getTenantId(), resultNode.getRowNumber(), resultNode.getTimePeriod()).block();
        if (!ObjectUtils.isEmpty(oldStandard) && oldStandard.getId() > 0) {
            resultNode.setId(oldStandard.getId());
        }
        this.prevYearCompRepository.save(resultNode).block();
    }

    @RabbitListener(queues = "quick-books-desktop.ProfitAndLoss-YTDComp.sync", concurrency = "6")
    public void processYtdComp(ProfitAndLossYTDComp resultNode) {
        this.saveYtdComp(resultNode);
    }

    private void saveYtdComp(ProfitAndLossYTDComp resultNode) {
        resultNode.setSyncTime(LocalDateTime.now());
        ProfitAndLossYTDComp oldStandard = this.ytdCompRepository.findByTenantIdAndRowNumberAndTimePeriod(
                resultNode.getTenantId(), resultNode.getRowNumber(), resultNode.getTimePeriod()).block();
        if (!ObjectUtils.isEmpty(oldStandard) && oldStandard.getId() > 0) {
            resultNode.setId(oldStandard.getId());
        }
        this.ytdCompRepository.save(resultNode).block();
    }


    @RabbitListener(queues = "quick-books-desktop.ProfitAndLoss-standard.sync", concurrency = "6")
    public void processStandard(ProfitAndLossStandard resultNode) {
        try {
            this.saveStandard(resultNode);
        } catch (Exception e) {
            log.error("Save standard data is error, msg: {}", e.getMessage());
        }
    }

    private void saveStandard(ProfitAndLossStandard resultNode) {
        resultNode.setSyncTime(LocalDateTime.now());
        ProfitAndLossStandard oldStandard = this.standardRepository.findByTenantIdAndRowNumberAndTimePeriod(
                resultNode.getTenantId(), resultNode.getRowNumber(), resultNode.getTimePeriod()).block();
        if (!ObjectUtils.isEmpty(oldStandard) && oldStandard.getId() > 0) {
            resultNode.setId(oldStandard.getId());
        }
        this.standardRepository.save(resultNode).block();
    }


    @RabbitListener(queues = "quick-books-desktop.ProfitAndLoss-detail.sync", concurrency = "6")
    public void processDetail(ProfitAndLossDetail resultNode) {
        this.saveDetail(resultNode);
    }

    private void saveDetail(ProfitAndLossDetail resultNode) {
        resultNode.setSyncTime(LocalDateTime.now());
        ProfitAndLossDetail oldStandard = this.detailRepository.findByTenantIdAndRowNumberAndTimePeriod(
                resultNode.getTenantId(), resultNode.getRowNumber(), resultNode.getTimePeriod()).block();
        if (!ObjectUtils.isEmpty(oldStandard) && oldStandard.getId() > 0) {
            resultNode.setId(oldStandard.getId());
        }
        this.detailRepository.save(resultNode).block();
    }
}
